package org.mobile.servlets;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mobile.beans.Phone;
import org.mobile.logic.PhoneLogic;

/**
 * Servlet implementation class GetPhones
 */
@WebServlet(name = "getPhones", urlPatterns = { "/getPhones" })
public class GetPhones extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetPhones() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if(request.getSession().getAttribute("phones") == null){ 
			List<Phone> phoneList= PhoneLogic.getInstance().getAllPhones();
			if(phoneList != null){
				request.getSession().setAttribute("phones", phoneList);
			}
			else{
				request.setAttribute("error", "Błąd wczytywania telefonów!");
				request.getRequestDispatcher("errPage.jsp").forward(request, response);
			}
		}
		response.sendRedirect("phones.jsp"); //i jest teraz tutaj
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
